<?php
/**
 * 根据车系名称获取匹配的系统ID值
 * @param $name
 * @return array
 */
function autoGetMatchedIds($name) {
	global $db;
	$sql = 'select * from xd_auto_category where name like "%' . $name . '%" and typename="rank"';
	$one = $db->getOne($sql);
	if ($one) {
		if ($one['level'] ==2 ) {
			$carid = $one['arid'];
			$manuid = AutoRank::getArmidByArcid($carid);
			$brandid = AutoRank::getArmidByArcid($carid);
		} else if ($one['level'] == 0) {
			$carid = '';
			$manuid = '';
			$brandid = $one['arid'];
		}
		return array(
			$brandid, $manuid, $carid
		);
	} else {
		return array(
			0,0,0
		);
	}
}

function match($kw) {
	$rule = DT_ROOT . '/file/dict/rules.utf8.ini' ;
	$dict = DT_ROOT . '/_tools/_dict/car.dict.utf8.xdb' ;
	$scws = new SCWS(SCWS::CHARSET_UTF8, $rule, $dict);
	$kw   = trim($kw);
	$scws->setText($kw);
	$kws   = $scws->getTops(5);
}

/**
 * 获取车系的文件夹名称
 * @param $carid
 * @return string
 */
function getCarFolder($carid, $prefix) {
	global $db;
	$cache = AutoCategory::getCache(AutoCategory::TYPE_RANK);
	$table = 'xd_auto_category';
	$root = array();
	$root[] = $prefix;
	$r = $db->getOne("select * from $table where arid = $carid");

	$parentids = explode(',',$r['arrparentid']);
	array_shift($parentids);
	foreach($parentids as $pid) {
		$root[] = str_replace(array('/','\\', '*', ':', '"','?','<','>', '|'), '',$cache[$pid]['name']);
	}
	$root[] = $r['name'];
	$folderUtf8 = implode('/', $root);
	return $folderUtf8;
}

/**
 * 获取车款的文件夹名称
 * @param $modelid
 * @return string
 */
function getModelFolder($modelid) {
	global $db;
	$table = 'xd_auto_model';
	$r = $db->getOne("select * from $table where amid = $modelid");
	return getCarFolder($r['arcid']).'/'.str_replace(array('/','\\', '*', ':', '"','?','<','>', '|'), '',$r['name']);
}

/**
 * 生成中文目录
 * @param $folder
 */
function mkZhFolder($folder) {
	if (!file_exists($folder)) {
		if (!mkdir($folder, 0777, true)) {
			exit($folder);
		}
	}
}

function getDir($dir) {
	$dirArray = NULL;
	if (false != ($handle = opendir($dir))) {
		$i = 0;
		while (false !== ($file = readdir($handle))) {
			//去掉"“.”、“..”以及带“.xxx”后缀的文件
			if ($file != "." && $file != "..") {
				$dirArray[$i] = $file;
				$i++;
			}
		}
		//关闭句柄
		closedir($handle);
	}
	return $dirArray;
}

//获取文件列表
function getFile($dir) {
	$fileArray[] = NULL;
	if (false != ($handle = opendir($dir))) {
		$i = 0;
		while (false !== ($file = readdir($handle))) {
			//去掉"“.”、“..”以及带“.xxx”后缀的文件
			if ($file != "." && $file != ".." && strpos($file, ".")) {
				$fileArray[$i] = $file;
				if ($i == 100) {
					break;
				}
				$i++;
			}
		}
		//关闭句柄
		closedir($handle);
	}
	return $fileArray;
}

function _($info){
	return mb_convert_encoding($info,'utf-8', 'gbk');
}

// 获取文件夹大小
function getDirSize($dir)
{
	$handle = opendir($dir);
	while (false!==($FolderOrFile = readdir($handle)))
	{
		if($FolderOrFile != "." && $FolderOrFile != "..")
		{
			if(is_dir("$dir/$FolderOrFile"))
			{
				$sizeResult += getDirSize("$dir/$FolderOrFile");
			}
			else
			{
				$sizeResult += filesize("$dir/$FolderOrFile");
			}
		}
	}
	closedir($handle);
	return $sizeResult;
}


function getFolderNew($folder) {
	$folder = str_replace('\\', '/', $folder);
	$folderPart = explode('/', $folder);
	$num = count($folderPart);
	$tmp = $folderPart[$num-2];
	$folderPart[$num-2] = $folderPart[$num-3];
	$folderPart[$num-3] = $tmp;
	return implode('/', $folderPart);
}